package LiKou;

public class Teat6 {
    public static void main(String[] args) {
        int[] gas = {2,3,4}, cost = {3,4,3};
        System.out.println(canCompleteCircuit(gas,cost));
    }
    public static int canCompleteCircuit(int[] gas, int[] cost) {
        int startIndex=-1;
        int count=0;
        int[] cha=new int[gas.length];
        for(int i=0;i<gas.length;i++){
            cha[i]=gas[i]-cost[i];
        }
        for(int left=0,right=0,sum=0;left<gas.length;){
            sum+=cha[right];
            count++;
            if(sum<0){
                while(sum<0 && left<=right){
                    sum-=cha[left];
                    left++;
                    count--;
                }
            }else{
                if(count==gas.length){
                    startIndex=left;
                    break;
                }
            }
            right=(right+1)%(gas.length);
        }
        return startIndex;
    }
}
